<template>
  <div class="dialog">
    <el-dialog
      :title="titleText"
      :visible.sync="showDialog"
      width="45%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="工序名称:"
              prop="productionProcessesName"
              class="w100p"
            >
              <el-input
                v-model="formData.productionProcessesName"
                clearable
                placeholder="请输入工序名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联产线:" prop="productionLineNo" class="w100p">
              <el-select
                v-model="formData.productionLineNo"
                placeholder="请选择"
                clearable
                @change="productionLineChange"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="关联加工设备:"
              prop="processesEquMapperList"
              class="w100p"
            >
              <el-select
                v-model="formData.processesEquMapperList"
                placeholder="请选择"
                multiple
                clearable
              >
                <el-option
                  v-for="item in EquOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
                <el-form-item
                  label="已关联加工设备:"
                  prop="processesEquMapperList"
                  class="w100p"
                >
                    <template v-for="item in devs">
                        <el-tag style="margin-right: 12px;">{{item}}</el-tag>
                    </template>
                </el-form-item>
          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from "@/utils/index";
export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    options: {
      type: Array,
      default: () => []
    },
    editData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      showDialog: this.show,
      // 展示
      EquOptions: [],
      formData: { processesEquMapperList: [] },
      pictureList: [],
      rules: {
        productionProcessesName: [
          {
            required: true,
            message: "请输入工序名称",
            trigger: ["change", "blur"]
          }
        ],
        productionLineNo: [
          {
            required: true,
            message: "请选择关联产线",
            trigger: ["change", "input", "blur"]
          }
        ],
        processesEquMapperList: [
          {
            type: "array",
            required: true,
            message: "请选择关联加工设备",
            trigger: ["change"]
          }
        ]
      }
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    }
  },
  created() {},
  mounted() {
    this.getformData();
    this.getEquOptions();
    this.$nextTick(() => {
      this.$refs.ruleForm.clearValidate();
    });
  },
  methods: {
    getformData() {
      this.formData = deepClone(this.editData);
    },
    // 获取EquOptions选项
    getEquOptions() {
      let self = this;
      this.EquOptions = [];
      this.options.forEach(item => {
        if (item.value === self.formData.productionLineNo) {
          item.Mapperlist.forEach(e => {
            self.EquOptions.push({
              name: e.equipmentName,
              value: e.equipmentCode
            });
          });
        }
      });
    },
    productionLineChange(val) {
      this.getEquOptions();
    },
    save(formData) {
      let self = this;
      this.$refs[formData].validate(valid => {
        if (valid) {
          console.log(this.formData);
          let list = [];
          self.formData.processesEquMapperList.forEach(e => {
            for (let i = 0; i < self.EquOptions.length; i++) {
              if (e === self.EquOptions[i].value) {
                list.push({
                  equipmentCode: self.EquOptions[i].value,
                  equipmentName: self.EquOptions[i].name
                });
              }
            }
          });
          this.formData.processesEquMapperList = list;
          this.$emit("addEvent", this.formData);
          this.close();
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("closeDialog");
      this.formData = {};
    }
  }
};
</script>

  <style lang="scss" scoped>
::v-deep
  .el-dialog__wrapper
  .el-dialog
  .el-dialog__body
  .el-form
  .el-form-item
  .el-form-item__label {
  min-width: 160px !important;
}
</style>
